package com.geek.gkpush.framework.threadpool.runnable;

import com.geek.gkpush.framework.trace.TraceContext;
import com.geek.gkpush.framework.trace.model.TraceInfo;

/**
 * <h1>Runnable 的抽象类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 *         ^_^
 * @version 0.0.1
 *         ^_^
 * @date 2023/10/2
 *
 */
public abstract class AbstractRunnable implements Runnable {

    private TraceInfo currTraceInfo;

    private static final String SIMPLE_NAME = AbstractRunnable.class.getSimpleName();

    public AbstractRunnable() {
        currTraceInfo = TraceContext.getOrCreate();
    }

    /**
     * 增强 irun 方法。
     */
    public abstract void irun();

    /**
     * 重写了 run 方法，内部实现了日志 TraceContext 上下文衔接操作。
     */
    @Override
    public void run() {
        TraceInfo subTrace = TraceContext.getCurrentTrace();
        try {
            TraceContext.start(currTraceInfo, subTrace, SIMPLE_NAME);

            irun();
        } finally {
            TraceContext.end(subTrace, SIMPLE_NAME);
        }
    }
}